package cn.iocoder.yudao.module.crossborder.crm.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.crossborder.crm.api.dto.CustomerDTO;
import cn.iocoder.yudao.module.crossborder.crm.service.CrmService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - 客户关系管理
 *
 * @author LHL
 */
@Tag(name = "管理后台 - 客户关系管理")
@RestController
@RequestMapping("/crossborder/crm")
@Validated
public class CrmController {

    @Resource
    private CrmService crmService;

    @GetMapping("/customer/get")
    @Operation(summary = "获得客户信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:crm:query')")
    public CommonResult<CustomerDTO> getCustomer(@RequestParam("id") Long id) {
        CustomerDTO customer = crmService.getCustomer(id);
        return success(customer);
    }

    @GetMapping("/customer/list")
    @Operation(summary = "获得客户信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('crossborder:crm:query')")
    public CommonResult<List<CustomerDTO>> getCustomerList(@RequestParam("ids") List<Long> ids) {
        List<CustomerDTO> customers = crmService.getCustomerList(ids);
        return success(customers);
    }

    @PostMapping("/customer/create")
    @Operation(summary = "创建客户信息")
    @PreAuthorize("@ss.hasPermission('crossborder:crm:create')")
    public CommonResult<Long> createCustomer(@RequestBody @Valid CustomerDTO customer) {
        Long id = crmService.createCustomer(customer);
        return success(id);
    }

    @GetMapping("/customer/validate")
    @Operation(summary = "校验客户是否存在")
    @Parameter(name = "customerId", description = "客户编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:crm:query')")
    public CommonResult<Boolean> validateCustomer(@RequestParam("customerId") Long customerId) {
        Boolean exists = crmService.validateCustomer(customerId);
        return success(exists);
    }

    @GetMapping("/customer/search")
    @Operation(summary = "搜索客户")
    @Parameter(name = "keyword", description = "搜索关键词", required = true, example = "张三")
    @PreAuthorize("@ss.hasPermission('crossborder:crm:query')")
    public CommonResult<List<CustomerDTO>> searchCustomers(@RequestParam("keyword") String keyword) {
        List<CustomerDTO> customers = crmService.searchCustomers(keyword);
        return success(customers);
    }

}
